package com.njtoyo.taxi.admin.rest.controller.business;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njtoyo.taxi.admin.aop.PassToken;
import com.njtoyo.taxi.admin.aop.PermissionScope;
import com.njtoyo.taxi.admin.rest.presenter.business.advertise.AdvertiseDetailsPresenter;
import com.njtoyo.taxi.admin.rest.presenter.business.advertise.AdvertisePagePresenter;
import com.njtoyo.taxi.admin.rest.wrapper.business.advertise.*;
import com.njtoyo.taxi.admin.service.business.advertise.AdvertiseService;
import com.njtoyo.taxi.entity.backend.AdminUser;
import com.taxi.entity.common.RestResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.io.File;

@RestController
@Api(value = "广告管理", tags = "广告管理")
@RequestMapping("business/advertise")
public class AdvertiseController {
    @Autowired
    private AdvertiseService advertiseService;

    @GetMapping("")
    @ApiOperation(value = "广告列表", notes = "广告列表,权限：advertisement_driver,advertisement_customer, advertisement_wxmini, advertisement_merchant_wxmini")
    @PermissionScope(names = {"advertisement_driver", "advertisement_customer", "advertisement_wxmini", "advertisement_merchant_wxmini"})
    RestResult<Page<AdvertisePagePresenter>> advertisePage(@ApiIgnore @ModelAttribute("admin") AdminUser adminUser,
                                                           @Valid AdvertiseWrapper wrapper) {
        return advertiseService.advertiseDriverPage(adminUser, wrapper);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "广告详情", notes = "广告详情,权限：advertisement_driver_update, advertisement_customer_update, advertisement_wxmini_update,advertisement_merchant_wxmini_update")
    @PermissionScope(names = {"advertisement_driver_update", "advertisement_customer_update", "advertisement_wxmini_update", "advertisement_merchant_wxmini_update"})
    RestResult<AdvertiseDetailsPresenter> advertiseDetails(@ApiIgnore @ModelAttribute("admin") AdminUser adminUser,
                                                           @PathVariable("id") Long id) {
        return advertiseService.advertiseDetails(adminUser, id);
    }

    @PostMapping("")
    @ApiOperation(value = "添加广告", notes = "添加广告，权限：advertisement_driver_create,advertisement_customer_create, advertisement_wxmini_create, advertisement_merchant_wxmini_create")
    @PermissionScope(names = {"advertisement_driver_create", "advertisement_customer_create", "advertisement_wxmini_create", "advertisement_merchant_wxmini_create"})
    RestResult add(@ApiIgnore @ModelAttribute("admin") AdminUser adminUser,
                   @Valid @RequestBody AdvertiseAddWrapper wrapper) {
        if (StrUtil.isBlank(wrapper.getMediaUrl()) || !wrapper.getMediaUrl().contains("njtoyo-taxi-admin")) {
            return RestResult.failed("请先上传广告素材");
        }
        return advertiseService.add(adminUser, wrapper);
    }

    @PatchMapping("/{id}")
    @ApiOperation(value = "修改广告", notes = "修改广告，权限：advertisement_driver_update, advertisement_customer_update, advertisement_wxmini_update,advertisement_merchant_wxmini_update")
    @PermissionScope(names = {"advertisement_driver_update", "advertisement_customer_update", "advertisement_wxmini_update", "advertisement_merchant_wxmini_update"})
    RestResult update(@ApiIgnore @ModelAttribute("admin") AdminUser adminUser,
                      @PathVariable("id") Long id,
                      @Valid @RequestBody AdvertiseUpdateWrapper wrapper) {
        if (StrUtil.isBlank(wrapper.getMediaUrl()) || !wrapper.getMediaUrl().contains("njtoyo-taxi-admin")) {
            return RestResult.failed("请先上传广告素材");
        }
        return advertiseService.update(adminUser, id, wrapper);
    }

    @PatchMapping("/batch")
    @ApiOperation(value = "批量处理广告", notes = "批量处理广告，权限：advertisement_driver_update, advertisement_customer_update, advertisement_wxmini_update,advertisement_merchant_wxmini_update")
    @PermissionScope(names = {"advertisement_driver_update", "advertisement_customer_update", "advertisement_wxmini_update", "advertisement_merchant_wxmini_update"})
    RestResult batchUpdateState(@ApiIgnore @ModelAttribute("admin") AdminUser adminUser,
                                @Valid @RequestBody BatchUpdateStateWrapper wrapper) {
        return advertiseService.batchUpdateState(adminUser, wrapper);
    }

    @DeleteMapping("")
    @ApiOperation(value = "删除广告", notes = "删除广告，权限：advertisement_driver_delete,advertisement_customer_delete, advertisement_wxmini_delete, advertisement_merchant_wxmini_delete")
    @PermissionScope(names = {"advertisement_driver_delete", "advertisement_customer_delete", "advertisement_wxmini_delete", "advertisement_merchant_wxmini_delete"})
    RestResult delete(@ApiIgnore @ModelAttribute("admin") AdminUser adminUser,
                      @Valid AdvertiseDeleteWrapper wrapper) {
        return advertiseService.delete(adminUser, wrapper);
    }


    /**
     * 传标题和图片地址到接口，返回url
     */
    @PostMapping("/point-to")
    @ApiOperation(value = "跳转地址", notes = "跳转地址，权限：无")
    RestResult<String> pointTo(@ApiIgnore @ModelAttribute("admin") AdminUser adminUser,
                               @Valid @RequestBody AdvertisePointToWrapper wrapper) {
        return advertiseService.pointTo(adminUser, wrapper);
    }


    @PostMapping("/upload")
    @PassToken
    RestResult upload(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return RestResult.failed("不能上传空文件");
        }
        return advertiseService.upload(file);
    }


    /**
     * 添加车机广告
     *
     * @return
     */
    @PostMapping("/addAuto")
    RestResult addAuto(@ApiIgnore @ModelAttribute("admin") AdminUser adminUser,
                       @Valid @RequestBody AdvertiseAutoAddWrapper wrapper) {
        return advertiseService.addAuto(adminUser, wrapper);
    }


    @PatchMapping("/updateAuto/{id}")
    RestResult updateAuto(@ApiIgnore @ModelAttribute("admin") AdminUser adminUser,
                          @PathVariable("id") Long id,
                          @Valid @RequestBody AdvertiseAutoAddWrapper wrapper) {
        return advertiseService.updateAuto(adminUser, id, wrapper);
    }


    /**
     * 获取车机广告总循环次数
     *
     * @return
     */
    @GetMapping("/getAutoTimes/{cantonId}")
    RestResult getAutoTimes(@PathVariable("cantonId") Long cantonId) {
        return advertiseService.getAutoTimes(cantonId);
    }


    @PostMapping("/saveAutoTimes")
    RestResult saveAutoTimes(@RequestBody Dict dict) {
        Long cantonId = dict.getLong("cantonId");
        Integer times = dict.getInt("times");
        if (ObjectUtil.isNull(cantonId)) {
            return RestResult.failed("请选择运营区");
        }
        if (ObjectUtil.isNull(times)) {
            return RestResult.failed("请配置循环次数");
        }
        return advertiseService.saveAutoTimes(cantonId, times);
    }


}
